<?php
/**
 * @version $Id$
 */
class AgentAction extends BaseAction {
	protected $agent;
	function _initialize() {
		parent::_initialize();
		self::exist_role(array(14,1));
		$this->agent=new Agent();
		
		//申请类型 1省专营商2市专营商3区专营商4代理商
		$agent_stype = array(1=>'省',2=>'市',3=>'区',4=>'代');
		$this->assign('agent_stype',$agent_stype);
		//支付状态 0未付款 1线上付款失败 2线下付款失败 3付款中 4线上已付款 5线下已付款
		$agent_pay_status = array(0=>'未付款',1=>'线上付款失败',2=>'线下付款失败',3=>'付款中',4=>'线上已付款',5=>'线下已付款');
		$this->assign('agent_pay_status',$agent_pay_status);
		//处理状态 0未审核 1审核未通过 2审核通过
		$agent_status = array(0=>'未审核',1=>'审核未通过',2=>'审核通过');
		$this->assign('agent_status',$agent_status);
		//开通状态 0未开通 1关闭帐户 2开通帐户
		$agent_isclose = array(0=>'未开通',1=>'关闭帐户',2=>'开通帐户');
		$this->assign('agent_isclose',$agent_isclose);
		//操作类型：1申请 2付款 3审核 4状态 5修改
		$log_type = array(1=>'申请',2=>'付款  ',3=>'审核 ',4=>'状态',5=>'修改');
		$this->assign('log_type',$log_type);
		
		$area=F('Area');
        if(empty($area)){
            $area=M('Area')->select();
            $area=field2key($area,'id');
            F('Area',$area);
        }		
        $this->assign('Area_list',$area);
	}
	/**
	 * 区域内所有套件
	 * */
    public function index(){
		//$condition=parent::create_where();
		$condition=array();
		
        if($this->keywords){
            $wherefields=explode(',',$_REQUEST['sfield_name']);
            if(in_array($_REQUEST['wherefields'], $wherefields)){
                $wherefields=$_REQUEST['wherefields'];
                $condition[$wherefields]=array("like",'%'.$this->keywords.'%');
                $this->assign('wherefields',$wherefields);
            }else{
            	$map=array();
                foreach ($wherefields as $value) {
                    $map[$value]=array("like",'%'.$this->keywords.'%');
                }
                $map['_logic'] = 'or';
                $condition['_complex'] = $map;
                $this->assign('wherefields','all');
            }
        }
		//商户类型条件查询
		$stype= intval($_REQUEST['stype']);
		if ($stype){
			$condition['stype'] = $stype;
		}
		//商户区域条件查询
		$vesting_province= intval($_REQUEST['vesting_province']);
		if ($vesting_province){
			$condition['vesting_province'] = $vesting_province;
		}
		$vesting_city= intval($_REQUEST['vesting_city']);
		if ($vesting_city){
			$condition['vesting_city'] = $vesting_city;
		}
		$vesting_area= intval($_REQUEST['vesting_area']);
		if ($vesting_area){
			$condition['vesting_area'] = $vesting_area;
		}
		//商户状态条件查询
		$pay_status = $_REQUEST[ 'pay_status' ];
         if ($pay_status != 'all' && !empty ($pay_status )){
               if ($pay_status == '-1' ){
                     $condition ['pay_status' ] = '0' ;
              } else {
                     $condition ['pay_status' ] = $pay_status;
              }
              
        }
         $status = $_REQUEST [ 'status'] ;
         if ($status != 'all' && !empty ($status )){
               if ($status == '-1' ){
                     $condition ['status' ] = '0' ;
              } else {
                     $condition ['status' ] = $status;
              }
        }
         $isclose = $_REQUEST [ 'isclose'] ;
         if ($isclose != 'all' && !empty ($isclose )){
               if ($isclose == '-1' ){
                     $condition ['isclose' ] = '0' ;
              } else {
                     $condition ['isclose' ] = $isclose;
              }
        }
		$this -> assign($_REQUEST);
		$order=$this->orderField.' '.$this->orderDirection;
		$totalRows = $this->agent->agent_Lower_list($condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->agent->agent_Lower_list($condition,$page->firstRow . ',' . $page->listRows ,$order);
			$this->assign('list',$list);
		}
		$this->display();
    }
	
	public function edit(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		if ($_REQUEST ['dosubmit']) {
			$_info = $_POST['info'];
			//13省12市10区22代理商
			$roleid = 0;
			//1省专营商2市专营商3区专营商4代理商
			switch ($_info['stype']) {
				case 1:
					$roleid = 13;
					break;
				case 2:
					$roleid = 12;
					break;
				case 3:
					$roleid = 10;
					break;
				case 4:
					$roleid = 22;
					break;
			}
			if(empty($roleid)||$roleid==0||!in_array($roleid, array(13,12,10,22))){
	            $this->_ajm(300,'分销商权限有误',0);
	        }
			$_info['starttime'] = strtotime($_POST['info']['starttime']);
			$_info['endtime'] = strtotime($_POST['info']['endtime']);
			$res=$this->agent->agent_edit($_info,array(),array(),$_info['user_id'],$this->userid);
			//修改商家平台-开通状态
			if(!empty($_info['isclose'])&&$_info['isclose']>0){
				$this->agent->agent_isclose($_info['id'],$_info['user_id'],$this->userid,$_info['isclose'],$roleid);
				if($_info['isclose']==2){
					$arr_area = array();
					$arr_area['province'] = $_info['vesting_province'];
					$arr_area['city'] = $_info['vesting_city'];
					$arr_area['area'] = $_info['vesting_area'];
					$this->agent->relation($_info['user_id'],$this->userid,$_info['stype'],$arr_area);
				}
			}
			$arr=object_array(json_decode($res));
			if($arr['status']==1){
				$this->_ajm(200,$arr['msg'],0,U ( 'Agent/index' ) );
			}else{
				$this->_ajm(300,$arr['msg'],0);
			}
		}
		$info = $this->agent->get_agent_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$this->assign('info',$info);
		$this->display();
	}
	public function profit(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		$this -> assign($_REQUEST);
		if ($_REQUEST ['dosubmit']) {
			$_info = $_POST['info'];
			$_info['agent_id'] = $id;
			$res = $this->agent->agent_profit_add($_info);
			$arr=object_array(json_decode($res));
			if($arr['status']==1){
				$json=array('statusCode'=>200,'dialogId'=>'Agent_profit_'.$id,'forwardUrl'=>U ( 'Agent/profit',array('id'=>$id)));
				exit(json_encode($json));
			}else{
				$this->_ajm(300,$arr['msg'],1);
			}
		}
        
        $pro_type = array();
		$pro_type['sj'] = '手机';
		$pro_type['pb'] = '平板';
		$pro_type['sjpj'] = '手机配件';
		$pro_type['sjsp'] = '手机饰品';
		$pro_type['sjlh'] = '手机靓号';
		$pro_type['hmpf'] = '号码批发';
		
		$condition = array();
		$condition['agent_id'] = $id;
		$this->listrows=$_REQUEST['numPerPage']?$_REQUEST['numPerPage']:5;
        $this->assign('numPerPage',$this->listrows);
		$totalRows = $this->agent->agent_profit_list($condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->agent->agent_profit_list($condition,$page->firstRow . ',' . $page->listRows);
			$this->assign('list',$list);
		}
		$this->assign('pro_type',$pro_type);
		$this->assign('info',$info);
		$this->display();
	}
	public function profitdel(){
		$id = intval ( $_REQUEST ['id'] );
		$agentid = intval ( $_REQUEST ['agentid'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		$res = $this->agent->agent_profit_del($id);
		$arr=object_array(json_decode($res));
		if($arr['status']==1){
			$json=array('statusCode'=>200,'dialogId'=>'Agent_profit_'.$id,'forwardUrl'=>U ( 'Agent/profit',array('id'=>$agentid)));
			exit(json_encode($json));
		}else{
			$this->_ajm(300,$arr['msg'],1);
		}
	}
	public function attached(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		$this -> assign($_REQUEST);
		if ($_REQUEST ['dosubmit']) {
			$arr_area = array(0=>'area',1=>'city',2=>'province');
			$primary_area = 0;
			$deputy_area = 0;
			foreach ($arr_area as $key => $value) {
				if(intval($_REQUEST [$value]) > 0 && $primary_area == 0){
					$primary_area = $_REQUEST [$value];
				}
			}
			foreach ($arr_area as $key => $value) {
				if(intval($_POST ['Agent_vesting_'.$value]) > 0 && $deputy_area == 0){
					$deputy_area = $_POST ['Agent_vesting_'.$value];
				}
			}
			$_info['primary_agent_id'] = $id;
			$_info['primary_area'] = $primary_area;
			$_info['deputy_agent_id'] = $_POST['Agent_user_id'];
			$_info['deputy_area'] = $deputy_area;
			$_info['action_userid'] = $this->userid;
			$_info['ctime'] = time();
			$res = $this->agent->agent_attached_add($_info);
			$arr=object_array(json_decode($res));
			if($arr['status']==1){
				$json=array('statusCode'=>200,'dialogId'=>'Agent_attached_'.$id,'forwardUrl'=>U ( 'Agent/attached',array('id'=>$id,'province'=>$_REQUEST ['province'],'city'=>$_REQUEST ['city'],'area'=>$_REQUEST ['area'])));
				exit(json_encode($json));
			}else{
				$this->_ajm(300,$arr['msg'],1);
			}
		}
		
		$condition = array();
		$condition['primary_agent_id'] = $id;
		$this->listrows=$_REQUEST['numPerPage']?$_REQUEST['numPerPage']:5;
        $this->assign('numPerPage',$this->listrows);
		$totalRows = $this->agent->agent_attached_list($condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->agent->agent_attached_list($condition,$page->firstRow . ',' . $page->listRows);
			$this->assign('list',$list);
		}
		$this->assign('info',$info);
		$this->display();
	}
	public function attacheddel(){
		$id = intval ( $_REQUEST ['id'] );
		$agentid = intval ( $_REQUEST ['agentid'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		$res = $this->agent->agent_attached_del($id);
		$arr=object_array(json_decode($res));
		if($arr['status']==1){
			$json=array('statusCode'=>200,'dialogId'=>'Agent_attached_'.$agentid,'forwardUrl'=>U ( 'Agent/attached',array('id'=>$agentid,'province'=>$_REQUEST ['province'],'city'=>$_REQUEST ['city'],'area'=>$_REQUEST ['area'])));
			exit(json_encode($json));
		}else{
			$this->_ajm(300,$arr['msg'],1);
		}
	}
	public function member(){
		$condition=array();
        //主表条件
        $sfield= $_REQUEST['sfield'];
        if(empty($_REQUEST['id'])){
			if ($sfield != 'all' && !empty ($sfield) && !empty ($this->keywords)){
				$condition['ut.'.$sfield] = $this->keywords;
			}
        }else{
        	$condition['ut.agent_id'] = intval($_REQUEST['id']);
        }
		//商户区域条件查询
		/*$province= intval($_REQUEST['province']);
		if ($province){
			$condition['ub.province'] = $province;
		}
		$city= intval($_REQUEST['city']);
		if ($city){
			$condition['ub.city'] = $city;
		}
		$area= intval($_REQUEST['area']);
		if ($area){
			$condition['ub.area'] = $area;
		}*/
		$this -> assign($_REQUEST);
		$order=$this->orderField.' '.$this->orderDirection;
		$totalRows = $this->agent->agent_member_list($condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->agent->agent_member_list($condition,$page->firstRow . ',' . $page->listRows);
			$this->assign('list',$list);
		}
		$this->display();
	}
	public function relation_all(){
		$res = $this->agent->relation_all($this->userid);
		$arr=object_array(json_decode($res));
		if($arr['status']==1){
			$this->_ajm(200,$arr['msg'],1,U ( 'Agent/index' ) );
		}else{
			$this->_ajm(300,$arr['msg'],1);
		}
	}
	public function relation(){
		$userid = intval ( $_REQUEST ['userid'] );
		$province = intval ( $_REQUEST ['province'] );
		$city = intval ( $_REQUEST ['city'] );
		$area = intval ( $_REQUEST ['area'] );
		$stype = intval ( $_REQUEST ['stype'] );
		$arr_area = array();
		$arr_area['province'] = $province;
		$arr_area['city'] = $city;
		$arr_area['area'] = $area;
		$res = $this->agent->relation($userid,$this->userid,$stype,$arr_area);
		$arr=object_array(json_decode($res));
		if($arr['status']==1){
			$this->_ajm(200,$arr['msg'],1,U ( 'Agent/index' ) );
		}else{
			$this->_ajm(300,$arr['msg'],1);
		}
	}
	
	public function detail(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据查看',0);
        }
		$info = $this->agent->get_agent_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$log = $this->agent->agent_log_list(array('userid'=>$info['user_id']));
		$this->assign('info',$info);
		$this->assign('log',$log);
		$this->assign("readonly",'disabled="true"');
		$this->display();
	}
	
	public function jiesuan(){
		$id = $_REQUEST['id'];
		$this->assign('id',$id);
		$this->display();
	}
}